var hasMore;
var next_cursor = 1;
var ul = $('#hot-trips');

function loadMoreTrips ( cursor ) {
    var reachBottom = document.body.scrollTop + window.innerHeight >= $(document).height() - 200;
    if ( reachBottom ) {
        $(document).off('scroll');
        getTrip( cursor );
    }
}

function getTrip ( cursor ) {
    var content = '';
    var _url ='/explore/?page='+ cursor;
    $.ajax({
        url: _url,
        type: 'GET',
        dataType: 'html',
        success: function(data) {
            $(data).appendTo(ul);
            checkNext();
        },
        error: function(xhr, textStatus) {
        }
    });
}

function checkNext () {
    hasMore = $('#mark').attr('data-more') === 'True';
    next_cursor = $('#mark').attr('data-next');
    $('#mark').remove();
    if ( hasMore ) {
        $(document).on("scroll", function () {
            loadMoreTrips(next_cursor);
        });
        $('#loading').show();
    }else{
        $('#loading').hide();
    }
}

$(document).ready(function() {
    checkNext();
});